package com.aigou.admin.controller;

import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.Receiving_address;
import com.aigou.admin.entity.ResponseEntity;
import com.aigou.admin.service.Receiving_addressservice;
import com.aigou.admin.service.impl.Receiving_addressserviceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/receiving_address")
public class Receiving_addressController extends BaseServlet {

    private Receiving_addressservice receiving_addressservice = new Receiving_addressserviceImpl();

    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        PageEntity<Receiving_address> receiving_addressPageEntity = receiving_addressservice.findReceiving_addressPage(Integer.valueOf(current),Integer.valueOf(pageSize));
        ResponseEntity.ok(resp,receiving_addressPageEntity);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Receiving_address> receiving_addresses = receiving_addressservice.findAll();
        ResponseEntity.ok(resp,receiving_addresses);
    }

    public void find(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Receiving_address receiving_address = receiving_addressservice.findByid(id);
        ResponseEntity.ok(resp,receiving_address);
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String id = req.getParameter("id");
        String receiving_address1 = req.getParameter("receiving_address");
        String receiving_person = req.getParameter("receiving_person");
        String mobile_phone = req.getParameter("mobile_phone");
        String user_id = req.getParameter("user_id");
        String is_default = req.getParameter("is_default");
        Receiving_address receiving_address = new Receiving_address(id,receiving_address1,receiving_person,mobile_phone,user_id,Integer.valueOf(is_default));
        boolean rs = receiving_addressservice.save(receiving_address);
        ResponseEntity.ok(resp,rs);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String id = req.getParameter("id");
        boolean rs = receiving_addressservice.delete(id);
        ResponseEntity.ok(resp,rs);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String receiving_address1 = req.getParameter("receiving_address");
        String receiving_person = req.getParameter("receiving_person");
        String mobile_phone = req.getParameter("mobile_phone");
        String user_id = req.getParameter("user_id");
        String is_default = req.getParameter("is_default");
        Receiving_address receiving_address = new Receiving_address(id, receiving_address1, receiving_person, mobile_phone, user_id, Integer.valueOf(is_default));
        boolean rs = receiving_addressservice.update(receiving_address);
        ResponseEntity.ok(resp, rs);
    }
}
